package com.kh.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kh.pojo.AjaxJson;
import com.kh.pojo.KHUser;
import com.kh.pojo.TableJson;
import com.kh.service.UserService;
import com.kh.utils.UserUtils;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;

@Controller
@RequestMapping("userController")
public class UserController {
	
	@Autowired
	UserService userService;
	
	@RequestMapping(params="list")
	public String list() {
		return UserUtils.THEME+"userList";
	}
	
	@RequestMapping(params="dataGrid")
	@ResponseBody
	public TableJson<KHUser> dataGrid(KHUser user){
		TableJson<KHUser> json=new TableJson<>();
		List<KHUser> userList = userService.getUserList(user);
		Long userCount = userService.getUserCount(user);
		json.setData(userList);
		json.setCount(userCount.intValue());
		return json;
	}
	@RequestMapping(params="goAdd")
	public String goAdd() {
		return UserUtils.THEME+"user-add";
	}
	
	@RequestMapping(params="doAdd")
	@ResponseBody
	public AjaxJson doAdd(KHUser user) {
		AjaxJson ajaxJson=new AjaxJson();
		ajaxJson.setCode(AjaxJson.ok);
		KHUser userByStaffNo = userService.getUserByStaffNo(user.getStaffNo());
		if(userByStaffNo!=null) {
			ajaxJson.setCode(AjaxJson.error);
			ajaxJson.setMessage("已经存在该用户");
			return ajaxJson;
		}
		userService.saveUser(user);
		return ajaxJson;
	}
	@RequestMapping(params="goChangePassword")
	public String goChangePassword() {
		return UserUtils.THEME+"user-changePassword";
	}
	
	@RequestMapping(params="doChangePassword")
	@ResponseBody
	public AjaxJson doChangePassword(String psd) {
		AjaxJson ajaxJson=new AjaxJson();
		ajaxJson.setCode(AjaxJson.ok);
		String staffNo = UserUtils.getStaffNo();
		
		return ajaxJson;
	}
	@RequestMapping(params="export")
	public void export(ModelMap map,HttpServletRequest request,HttpServletResponse response) {
		List<KHUser> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
        		KHUser user = new KHUser();
            user.setStaffNo("x-"+i);
            user.setName("张三");
            list.add(user);
        }
        ExportParams params = new ExportParams(null, "测试", ExcelType.XSSF);
//        params.setFreezeCol(2);
        map.put(NormalExcelConstants.DATA_LIST, list);
        map.put(NormalExcelConstants.CLASS, KHUser.class);
        map.put(NormalExcelConstants.PARAMS, params);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
	}
}
